# -*- coding: utf-8 -*-
# created on 2016/11/10

from mathsolver.functions.base import BaseFunction, BaseNumber, default_symbol, new_latex
from sympy import Poly, sympify


# 函数奇偶性与方程的根
class JiOuFanChengGen(BaseFunction):
    def solver(self, *args):
        func = args[1]
        number, f_var, f_name = args[0].value, func.var, func.name
        symbol = default_symbol(f_var)
        a, b = Poly(f_var).all_coeffs()
        self.steps.append(["依题意知函数图像关于原点对称", "所以图像与 x 轴的交点关于原点对称"])

        result = - b * number / a

        # 如果问题是 f(ax + b)
        if b != 0:
            step1 = ", ".join(["x%s" % i for i in range(1, number + 1)])
            step2 = " + ".join([new_latex(f_var.subs(symbol, sympify("x%s" % i))) for i in range(1, number + 1)])
            step3 = " + ".join(["x%s" % i for i in range(1, number + 1)])

            self.steps.append(["设图像与 x 轴交点的横坐标为 {}".format(step1),
                               "根据对称性可知 {} = 0".format(step2)])
            self.steps.append(["", "则 {} = {}".format(step3, result)])

        self.steps.append(["", "所以 {} 的所有根的和为 {}".format(func.printing(), new_latex(result))])
        self.label.add("函数奇偶性与方程的根")
        self.output.append(BaseNumber(result))

        return self


if __name__ == '__main__':
    pass
    # 已知偶函数f(x)的图象与x轴有五个公共点,那么方程f(x)=0的所有实根之和为().
